package com.example.sixtrainingdemo.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.AbstractWindowedCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.sixtrainingdemo.database.DBHelper;

public class ZNBProvider extends ContentProvider {

   private static final String AUTHORITY = "com.example.sixtrainingdemo.provider";
   private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
   public static final int MATCHER_GOODS = 1;
   private static final int MATCHER_GOODS_ID = 2;
   private static final int MATCHER_USER = 11;
   private static final int MATCHER_USER_ID = 12;
   private static final int MATCHER_PHOTO = 21;
   private static final int MATCHER_ROUND = 22;
   private static final int MATCHER_ROUND_ID = 23;
   private static final int MATCHER_ROUNDPHOTO = 24;
   private static final int MATCHER_TALK = 26;
   private static final int MATCHER_SHOP = 28;
   private DBHelper mDBHelper;

   static {//通配符：*所有字符，#所有数字
      // content://com.jlastu.znb.provider/goods
      uriMatcher.addURI(AUTHORITY, "goods", MATCHER_GOODS);
      // content://com.jlastu.znb.provider/goods/1
      uriMatcher.addURI(AUTHORITY, "goods/#", MATCHER_GOODS_ID);
      uriMatcher.addURI(AUTHORITY, "user", MATCHER_USER);
      uriMatcher.addURI(AUTHORITY, "user/#", MATCHER_USER_ID);
      uriMatcher.addURI(AUTHORITY, "photo", MATCHER_PHOTO);
      uriMatcher.addURI(AUTHORITY, "round", MATCHER_ROUND);
      uriMatcher.addURI(AUTHORITY, "round/#", MATCHER_ROUND_ID);
      uriMatcher.addURI(AUTHORITY, "roundphoto", MATCHER_ROUNDPHOTO);
      uriMatcher.addURI(AUTHORITY, "talk", MATCHER_TALK);
      uriMatcher.addURI(AUTHORITY, "shop", MATCHER_SHOP);
   }

   @Override
   public boolean onCreate() {
      mDBHelper = new DBHelper(getContext());
      return false;
   }

   @Nullable
   @Override
   public Cursor query(@NonNull Uri uri,
                       @Nullable String[] projection,
                       @Nullable String selection,
                       @Nullable String[] selectionArgs,
                       @Nullable String sortOrder) {
      SQLiteDatabase db = mDBHelper.getReadableDatabase();
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:
            return db.query("goods", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_GOODS_ID:
            long goods_id = ContentUris.parseId(uri);
            return db.query("goods", projection, "_id = ?",
                    new String[]{String.valueOf(goods_id)}, null,
                    null, sortOrder, null);
         case MATCHER_USER:
            return db.query("user", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_USER_ID:
            long user_id = ContentUris.parseId(uri);
            Log.d("provider", "user_id ========= " + user_id);
            return db.query("user", projection, "_id = ?",
                    new String[]{String.valueOf(user_id)}, null,
                    null, sortOrder, null);
         case MATCHER_PHOTO:
            Cursor cursor = db.query("photo", projection, selection,
                    selectionArgs, null, null, sortOrder);
            CursorWindow cw = new CursorWindow("test", 5000000);
            AbstractWindowedCursor ac = (AbstractWindowedCursor) cursor;
            ac.setWindow(cw);
            return cursor;
         case MATCHER_ROUND:
            return db.query("round", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_ROUND_ID:
            long round_id = ContentUris.parseId(uri);
            return db.query("round", projection, "_id = ?",
                    new String[]{String.valueOf(round_id)}, null,
                    null, sortOrder, null);
         case MATCHER_ROUNDPHOTO:
            Cursor cursor1 = db.query("roundphoto", projection, selection,
                    selectionArgs, null, null, sortOrder);
            CursorWindow cww = new CursorWindow("test", 5000000);
            AbstractWindowedCursor acc = (AbstractWindowedCursor) cursor1;
            acc.setWindow(cww);
            return cursor1;
         case MATCHER_TALK:
            return db.query("talk", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_SHOP:
            return db.query("shop", projection, selection,
                    selectionArgs, null, null, sortOrder);
      }
      return null;
   }

   @Nullable
   @Override
   public String getType(@NonNull Uri uri) {
      return null;
   }

   @Nullable
   @Override
   public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      long insertId = -1;
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:
            insertId = db.insert("goods", null, values);
            break;
         case MATCHER_USER:
            insertId = db.insert("user", null, values);
            break;
         case MATCHER_PHOTO:
            insertId = db.insert("photo", null, values);
            break;
         case MATCHER_ROUND:
            insertId = db.insert("round", null, values);
            break;
         case MATCHER_ROUNDPHOTO:
            insertId = db.insert("roundphoto", null, values);
            break;
         case MATCHER_TALK:
            insertId = db.insert("talk", null, values);
            break;
         case MATCHER_SHOP:
            insertId = db.insert("shop", null, values);
            break;
      }
      return ContentUris.withAppendedId(uri, insertId);
   }

   @Override
   public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
      for (ContentValues contentValues : values) {
         Log.d("", "bulkInsert ===== ");
         insert(uri, contentValues);
      }
      return super.bulkInsert(uri, values);
   }

   @Override
   public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:

         case MATCHER_GOODS_ID:
            long goods_id = ContentUris.parseId(uri);

         case MATCHER_USER:

         case MATCHER_USER_ID:
            long user_id = ContentUris.parseId(uri);

         case MATCHER_ROUND:

         case MATCHER_ROUND_ID:
            long round_id = ContentUris.parseId(uri);

      }
      return 0;
   }

   @Override
   public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:

         case MATCHER_GOODS_ID:
            long goods_id = ContentUris.parseId(uri);

         case MATCHER_USER:

         case MATCHER_USER_ID:
            long user_id = ContentUris.parseId(uri);

      }
      return 0;
   }
}
